package com.biz.primus.ms.order.convertor;

import com.biz.primus.base.exception.BizSilentException;
import com.biz.primus.model.oms.enums.OrderDeliveryState;
import com.biz.primus.model.oms.enums.OrderDeliveryType;
import com.biz.primus.model.oms.enums.OrderPaymentState;
import com.biz.primus.model.oms.enums.OrderState;
import com.biz.primus.model.oms.vo.*;
import com.biz.primus.model.order.enums.DeliveryType;
import com.biz.primus.model.order.exception.OrderException;
import com.biz.primus.ms.order.dao.po.Order;
import com.biz.primus.ms.order.dao.po.OrderItem;
import com.biz.primus.ms.order.dao.po.embeddable.OrderConsigneeInfo;
import com.biz.primus.ms.order.dao.po.embeddable.OrderDeliveryInfo;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 中台订单转换器
 *
 * @author Tiger Feng
 * @date 2017年11月15日
 */
public class OmsOrderConvertor {

    /**
     * 新支付订单转中台订单模型
     *
     * @param po
     * @return
     */
    public static OmsOrderVo newPaidPo2OmsOrderVo(Order po) {
        if (po == null) {
            return null;
        }
        OmsOrderVo vo = new OmsOrderVo();
        OrderDeliveryInfo delivery = po.getDelivery();
        vo.setCode(po.getOrderCode());
        vo.setType("orders");
        vo.setState(OrderState.begin);
        // 支付状态
        vo.setPaymentState(OrderPaymentState.paid);
        // 支付记录
        List<OmsPaymentVo> payments = Collections.emptyList();
        OmsPaymentVo omsPaymentVo = new OmsPaymentVo();
        omsPaymentVo.setPaidAmount(po.getPayableAmount());
        omsPaymentVo.setPaidTime(po.getPaymentTimestamp().toLocalDateTime());
        omsPaymentVo.setPaymentWay(po.getPaymentWay().getValue() + "");
        omsPaymentVo.setTradeNo(po.getPaidSerialNumber());
        payments.add(omsPaymentVo);
        vo.setPayments(payments);
        // 渠道
        vo.setChannel("");
        vo.setOrderSource(po.getClient().toString());
        vo.setDepot(po.getDepotCode());
        if (delivery.getDeliverType() == DeliveryType.USER_TRANSPORT) {
            vo.setDepot(po.getDepotCode());
        }
        vo.setLiquidated(po.getPayableAmount());
        vo.setAmount(po.getPayableAmount());
        vo.setFreightAmount(delivery.getDeliveryMoney());
        vo.setDiscountAmount(po.getDiscountCouponMoneyTotal() + po.getDiscountPromotionMoneyTotal());
        vo.setDiscountPop(po.getDiscountPromotionMoneyTotal());
        vo.setDiscountCoupon(po.getDiscountCouponMoneyTotal());

        vo.setUserId(po.getUserId());
        switch (delivery.getDeliverType()) {
            case STORE_TRANSPORT:
                if (po.getDelivery().getIsAppointment()) {
                    vo.setDeliveryType(OrderDeliveryType.self_appointment);
                } else {
                    vo.setDeliveryType(OrderDeliveryType.self_now);
                }
                break;
            case USER_TRANSPORT:
                vo.setDeliveryType(OrderDeliveryType.take_their);
                break;
            case THRID_TRANSPORT:
                vo.setDeliveryType(OrderDeliveryType.thrid_party);
                break;
            default:
                throw new BizSilentException(OrderException.NOT_SUPPORT_DELIVEY_TYPE);
        }
        // 第三方物流供应商
        // 留空
        vo.setExpressNumber(delivery.getExpressNumber());
        switch (delivery.getDeliveryState()) {
            case SIGNED:
                vo.setDeliveryState(OrderDeliveryState.signed);
                break;
            case PART_OF_SIGNED:
                vo.setDeliveryState(OrderDeliveryState.part_of_signed);
                break;
            case REJECT:
                vo.setDeliveryState(OrderDeliveryState.reject);
                break;
            case DISTRIBUTING:
                vo.setDeliveryState(OrderDeliveryState.distributing);
                break;
            case FOR_OUTBOUND:
                vo.setDeliveryState(OrderDeliveryState.for_outbound);
                break;
            case FOR_TAKE_THEIR:
                vo.setDeliveryState(OrderDeliveryState.for_take_their);
                break;
            default:
                break;
        }
        vo.setSaleTime(po.getCreateTimestamp().toLocalDateTime());
        vo.setExpectedReceivingTime(delivery.getExpectDeliveryTimestamp().toLocalDateTime());
        // 配送时效
        switch (delivery.getPeriods()) {
            case now:
                vo.setDeliveryTimeliness("in19m");
                break;
            case half_hour:
                vo.setDeliveryTimeliness("in29m");
                break;
            case one_hour:
                vo.setDeliveryTimeliness("in59m");
                break;
            default:
                break;
        }
        // 收货人信息
        vo.setConsignee(consigneeInfo2OmsConsigneeVo(po.getConsignee()));
        vo.setRemark(po.getUserRemark());
        vo.setSelfRemark(po.getSelfRemark());
        // 订单项
        vo.setItems(po.getItems()
                .stream()
                .map(orderItem -> orderItem2OmsItemVo(orderItem))
                .collect(Collectors.toList()));
        return vo;
    }

    private static OmsConsigneeVo consigneeInfo2OmsConsigneeVo(OrderConsigneeInfo po) {
        if (po == null) {
            return null;
        }
        OmsConsigneeVo vo = new OmsConsigneeVo();
        vo.setConsigneeName(po.getConsigneeName());
        vo.setConsigneeMobile(po.getConsigneeMobile());
        vo.setAddress(po.getConsigneeAddress());
        vo.setLat(po.getConsigneeLat());
        vo.setLon(po.getConsigneeLon());
        vo.setProvinceId(po.getConsigneeProvinceId());
        vo.setCityId(po.getConsigneeCityId());
        vo.setDistrictId(po.getConsigneeDistrictId());
        return vo;
    }

    private static OmsItemVo orderItem2OmsItemVo(OrderItem po) {
        if (po == null) {
            return null;
        }
        OmsItemVo vo = new OmsItemVo();
        vo.setItemCode(po.getItemCode());
        vo.setCode(po.getProductCode());
        vo.setCategory(po.getCategoryId() + "");
        vo.setBrand(po.getBrandId() + "");
        vo.setName(po.getProductName());
        vo.setQuantity(po.getQuantity());
        vo.setPrice(po.getMemberPrice());
        vo.setFinalPrice(po.getFinalPrice());
        vo.setMarketPrice(po.getMarketPrice());
        vo.setDiscountAmount(po.getDiscountCouponMoney() + po.getDiscountPromotionMoney());
        vo.setDiscountPop(po.getDiscountPromotionMoney());
        vo.setDiscountCoupon(po.getDiscountCouponMoney());
        vo.setWeight(po.getWeight());
        // 促销活动分摊信息
        // 优惠券分摊信息
        return vo;
    }

    /**
     * 获取OMS订单状态处理接口请求视图
     * @param po
     * @return
     */
    public static UserOrderOperationRequest getOmsOrderOperationRequestVoFromPo(Order po){
        if (po == null) {
            return null;
        }
        UserOrderOperationRequest vo = new UserOrderOperationRequest();
        vo.setOrderCode(po.getOrderCode());
        vo.setUserId(po.getUserId());
        return vo;
    }

}
